﻿
using MediatR;
using Traceability.App.Api.Application.Command.Detail.Cultivation;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model.Cultivation;

namespace Traceability.App.Api.Application.Handler.Detail.Cultivation
{
    public class GetCultivationHandler : IRequestHandler<GetCultivationCommand, ApiResult<CultivationModel>>
    {
        IBaseRepository<CultivationModel> culRepo;

        public GetCultivationHandler(IBaseRepository<CultivationModel> culRepo)
        {
            this.culRepo = culRepo;
        }

        public Task<ApiResult<CultivationModel>> Handle(GetCultivationCommand request, CancellationToken cancellationToken)
        {
            ApiResult<CultivationModel> res=new ApiResult<CultivationModel>();
            var ssss = culRepo.GetAll().Where(c=> c.Batch == request.Batch).OrderByDescending(c=>c.Id).FirstOrDefault();
            if (ssss==null)
            {
                res.Msg = "错误";
                res.Code = ApiEnum.Fail;
                return Task.FromResult(res);
            }
            res.Msg = "成功";
            res.Code = ApiEnum.Success;
            res.Datas = ssss;
            return Task.FromResult(res);
        }
    }
}
